using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;


using MM.Business;
using MM.Entities;

namespace MM
{
    public partial class frmHienthiQuanHePRPO : Form
    {
        string _sConn = "";

        public frmHienthiQuanHePRPO(string sConn)
        {
            InitializeComponent();
            _sConn = sConn;
        }

        private List<CHI_TIET_PDH_PYC> TimQuanHePRPO_FromPO(string POThamChieuSAP)
        {
            clsCHI_TIET_PDH_PYC ctCtrl = new clsCHI_TIET_PDH_PYC(_sConn);
            List<CHI_TIET_PDH_PYC> l = ctCtrl.DanhsachCHI_TIET_PDH_PYCByPDH(POThamChieuSAP);
            return l;
        }

        private List<CHI_TIET_PDH_PYC> TimQuanHePRPO_FromPR(string MaPhieuYeuCau)
        {
            clsCHI_TIET_PDH_PYC ctCtrl = new clsCHI_TIET_PDH_PYC(_sConn);
            List<CHI_TIET_PDH_PYC> l = ctCtrl.DanhsachCHI_TIET_PDH_PYCByPYC(MaPhieuYeuCau);
            return l;
        }

        private void HienthiPHIEU_DAT_HANG(string POThamChieuSAP)
        {
            clsPHIEU_DAT_HANG pdhCtrl = new clsPHIEU_DAT_HANG(_sConn);
            List<PHIEU_DAT_HANG> lst = new List<PHIEU_DAT_HANG>();
            lst = pdhCtrl.usp_SelectPHIEU_DAT_HANG(POThamChieuSAP);
            if (lst.Count > 0)
            {
                this.txtSoPhieuPO.Text = lst[0].POThamChieuSAP;
                this.txtPlant.Text = lst[0].PlanReferenceSAP;
                this.txtNguoiThucHien.Text = lst[0].NguoiNhap;
                this.txtNgayThucHien.Text = lst[0].NgayNhap.Day.ToString() + "/" + lst[0].NgayNhap.Month.ToString() + "/" + lst[0].NgayNhap.Year.ToString();               
            }
        }
        private void HienthiCHI_TIET_PDH(string POThamChieuSAP)
        {
            clsCHI_TIET_PDH ctCtrl = new clsCHI_TIET_PDH(_sConn);
            List<CHI_TIET_PDH> lst = new List<CHI_TIET_PDH>();
            lst = ctCtrl.usp_SelectCHI_TIET_PDHByPOThamChieuSAP(POThamChieuSAP);
            this.gridControl2.DataSource = lst;
            // liet ke chi tiet cac phieu yeu cau co moi quan he voi PO nay
            
            List<CHI_TIET_PDH_PYC> lstquanhe = new List<CHI_TIET_PDH_PYC>();
            lstquanhe = this.TimQuanHePRPO_FromPO(POThamChieuSAP);
            if (lstquanhe.Count>0)
            {
                List<CHI_TIET_PYC> lstCT = new List<CHI_TIET_PYC>();
                List<CHI_TIET_PYC> temp ;
                clsCHI_TIET_PYC ctCtrl1 = new clsCHI_TIET_PYC(_sConn);
                clsCHI_TIET_PDH_COMPONENT comCtrl = new clsCHI_TIET_PDH_COMPONENT(_sConn);
                List<CHI_TIET_PDH_COMPONENT> lstCom;

                for (int j = 0; j < lstquanhe.Count; j++)
                {
                    temp = new List<CHI_TIET_PYC>();
                    temp = ctCtrl1.SelectCHI_TIET_PYCByMaPhieuYeuCau(lstquanhe[j].MaPYC);

                    for (int i = 0; i < temp.Count; i++)
                    {
                        if (temp[i].MaTrangThai == 1)// gia cong
                        {
                            lstCom = new List<CHI_TIET_PDH_COMPONENT>();
                            lstCom = comCtrl.SelectCHI_TIET_PYC_COMPONENT(temp[i].MaChiTiet);
                            temp[i].lstComponent = lstCom;
                        }

                        lstCT.Add(temp[i]);
                    }
                }

                this.gridControl1.DataSource = lstCT;
            }       
                        
        }

        private void HienthiPHIEU_YEU_CAU(string _maphieuyeucau)
        {
            clsPHIEU_YEU_CAU pycCtrl = new clsPHIEU_YEU_CAU(_sConn);
            List<PHIEU_YEU_CAU> lst = new List<PHIEU_YEU_CAU>();
            lst = pycCtrl.SelectPHIEU_YEU_CAU(_maphieuyeucau);
            if (lst.Count > 0)
            {
                this.txtMaPhieuYeuCau.Text = lst[0].MaPhieuYeuCau;
                this.txtNguoiThucHien.Text = lst[0].NguoiTao;
                this.txtNgayThucHien.Text = lst[0].NgayTao.Day.ToString() + "/" + lst[0].NgayTao.Month.ToString() + "/" + lst[0].NgayTao.Year.ToString();
                if (lst[0].SoLanExport > 0)
                    chkDaduyet.Checked = true;
                else
                    chkDaduyet.Checked = false;

                if (lst[0].TinhTrang == 1)
                    chkXoa.Checked = true;
                else
                    chkXoa.Checked = false;
            }
        }
        private void HienthiCHI_TIET_PYC(string _maphieuyeucau)
        {
            List<CHI_TIET_PYC> lstCT = new List<CHI_TIET_PYC>();

            clsCHI_TIET_PYC ctCtrl = new clsCHI_TIET_PYC(_sConn);
            clsCHI_TIET_PDH_COMPONENT comCtrl = new clsCHI_TIET_PDH_COMPONENT(_sConn);
            List<CHI_TIET_PDH_COMPONENT> lstCom;

            lstCT = ctCtrl.SelectCHI_TIET_PYCByMaPhieuYeuCau(_maphieuyeucau);
            for (int i = 0; i < lstCT.Count; i++)
            {
                if (lstCT[i].MaTrangThai == 1)// gia cong
                {
                    lstCom = new List<CHI_TIET_PDH_COMPONENT>();
                    lstCom = comCtrl.SelectCHI_TIET_PYC_COMPONENT(lstCT[i].MaChiTiet);
                    lstCT[i].lstComponent = lstCom;
                }
            }
            this.gridControl1.DataSource = lstCT;

            // liet ke chi tiet cac phieu yeu cau co moi quan he voi PO nay
            
            List<CHI_TIET_PDH_PYC> lstquanhe = new List<CHI_TIET_PDH_PYC>();
            lstquanhe = this.TimQuanHePRPO_FromPR(_maphieuyeucau);
            if (lstquanhe.Count > 0)
            {
                clsCHI_TIET_PDH ctCtrl1 = new clsCHI_TIET_PDH(_sConn);
                List<CHI_TIET_PDH> lst = new List<CHI_TIET_PDH>();
                List<CHI_TIET_PDH> temp;
                for (int i = 0; i < lstquanhe.Count; i++)
                {
                    temp = new List<CHI_TIET_PDH>();
                    temp = ctCtrl1.usp_SelectCHI_TIET_PDHByPOThamChieuSAP(lstquanhe[i].POThamChieuSAP);
                    for (int j = 0; j < temp.Count; j++)
                    {
                        lst.Add(temp[j]);
                    }
                }
                this.gridControl2.DataSource = lst;
            }
        }

       

        private void label1_Click(object sender, EventArgs e)
        {
            frmTimPhieuYC f = new frmTimPhieuYC(_sConn);
            f.chonPYC += new frmTimPhieuYC.ChonPhieuYeuCau(GetPhieuYeuCau);
            f.Show(this);
        }
        private void GetPhieuYeuCau(object sender, ChonPhieuYCEventArgs e)
        {        
            this.HienthiCHI_TIET_PYC(e.MaPhieuYeuCau);            
        }

        private void label6_Click(object sender, EventArgs e)
        {
            frmTimPhieuDH f = new frmTimPhieuDH(_sConn);
            f.chonPDH += new frmTimPhieuDH.ChonPhieuDatHang(GetPhieuDatHang);
            f.Show(this);
        }
        private void GetPhieuDatHang(object sender, ChonPhieuDHEventArgs e)
        {
            HienthiPHIEU_DAT_HANG(e.POThamChieuSAP);
            HienthiCHI_TIET_PDH(e.POThamChieuSAP);           
        }

        private void txtMaPhieuYeuCau_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.txtMaPhieuYeuCau.Text.Trim().Length == 10)
                {
                    this.HienthiPHIEU_YEU_CAU(this.txtMaPhieuYeuCau.Text.Trim());
                    this.HienthiCHI_TIET_PYC(this.txtMaPhieuYeuCau.Text.Trim());
                }
            }
        }

        private void txtSoPhieuPO_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (this.txtSoPhieuPO.Text.Trim().Length == 10)
                {
                    this.HienthiPHIEU_DAT_HANG(this.txtSoPhieuPO.Text.Trim());
                    this.HienthiCHI_TIET_PDH(this.txtSoPhieuPO.Text.Trim());
                }
            }
        }

        private void gridDSVT_ColumnFilterChanged(object sender, EventArgs e)
        {
            try
            {
                GridView view = (GridView)sender;
                ColumnView col = (ColumnView)this.gridControl1.FocusedView;
                if (view.FocusedColumn.Name == "colMaVTPT")
                {
                  
                    DevExpress.Data.Filtering.CriteriaOperator cri=gridDSVT.ActiveFilterCriteria;                

                 

                    gridDSVT1.Columns["MaThietBi"].FilterInfo = new DevExpress.XtraGrid.Columns.ColumnFilterInfo(cri);
                    

                    gridDSVT1.RefreshData();
                }
            }
            catch { }
        }

        private void frmHienthiQuanHePRPO_Load(object sender, EventArgs e)
        {
            Common.SetColor(this);
        }

        private void gridDSVT1_ColumnFilterChanged(object sender, EventArgs e)
        {
            try
            {
                GridView view = (GridView)sender;
                ColumnView col = (ColumnView)this.gridControl2.FocusedView;
                if (view.FocusedColumn.Name == "gridColumn8")
                {

                    DevExpress.Data.Filtering.CriteriaOperator cri = gridDSVT1.ActiveFilterCriteria;



                    gridDSVT.Columns["MaThietBi"].FilterInfo = new DevExpress.XtraGrid.Columns.ColumnFilterInfo(cri);


                    gridDSVT.RefreshData();
                }
            }
            catch { }
        }

        

    }
}